package cn.edu.fudan.bdke.ncov.qa.util;

import edu.stanford.nlp.ie.crf.CRFClassifier;
import edu.stanford.nlp.ling.CoreLabel;

import java.util.List;
import java.util.Properties;

/**
 * 中文自然语言处理工具类
 *
 * @author xym
 * @since 1.0
 */
public class ChineseNlp {
    private static CRFClassifier<CoreLabel> segmenter;
    private static final String SEGMENTER_BASE_DIR = "src/main/resources/stanfordNlpToolkit/segmenter/data";

    private ChineseNlp() {

    }

    static {
        initDefaultSegmenter();
    }

    /**
     * 中文分词
     *
     * @param doc sentences
     * @return 每个词语用String表示，返回所有的词语
     */
    public static List<String> segmentWord(String doc) {
        return segmenter.segmentString(doc);
    }

    public static void initDefaultSegmenter() {
        if (segmenter == null) {
            Properties props = new Properties();
            props.setProperty("sighanCorporaDict", SEGMENTER_BASE_DIR);
            props.setProperty("serDictionary", SEGMENTER_BASE_DIR + "/dict-chris6.ser.gz," + SEGMENTER_BASE_DIR + "/custom-dict.ser.gz");
            props.setProperty("inputEncoding", "UTF-8");
            props.setProperty("sighanPostProcessing", "true");
            segmenter = new CRFClassifier<>(props);
            segmenter.loadClassifierNoExceptions(SEGMENTER_BASE_DIR + "/ctb.gz", props);
        }
    }
}
